package com.serkol.ams.controller.api;


import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.Shop;
import com.serkol.ams.service.ShopService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Hesenjan(Serkol)
 * @since 2021-11-17
 */

@RestController
@RequestMapping("api/shop")
public class ShopApiController {
    @Autowired
    private ShopService service;

    @Value("${upload.host}")
    private String uploadhost;

    @IsLogin
    @GetMapping("findByList")
    public Result findByList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current) {
        AuthConstant.setPathUrl(uploadhost);
        return service.findByList(pageSize, current);

    }

    @IsLogin
    @GetMapping("list")
    public Result list() {
        AuthConstant.setPathUrl(uploadhost);
        List<Map<String, Object>> list = service.miniList();
        return Result.ok(list);
    }


    @IsLogin
    @PostMapping("edit")
    public Result edit(@RequestBody Shop data) {


        boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {

            flag = service.updateById(data);
        } else {


            flag = service.save(data);

        }

        return flag ? Result.ok() : Result.failure(ResultConstant.FAIL);


    }


}

